#include <bits/stdc++.h>
using namespace std;
const int N=6e5+5;
struct node
{
    int id;
    long long v;
    bool operator<(node b) const
    {
        return v<b.v;
    }
};
long long a[N],ans;
int l[N],r[N],f[N],n,k;
priority_queue<node>q;
int main() 
{
    #ifdef LOCAL
        freopen("data.in", "r", stdin);
        freopen("data.out", "w", stdout);
        auto start_time = chrono::high_resolution_clock::now();
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    // ================ 你的代码从这里开始 ================
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i],q.push(node{i,a[i]}),l[i]=i-1,r[i]=i+1;
    int tot=n;
    for(int i=1;i<=k;i++)
    {
        while(q.size()&&f[q.top().id]) q.pop();
        if(!q.size()||q.top().v<0) break;
        node t=q.top();q.pop();
        ans+=t.v;
        int u=t.id,last=l[t.id],next=r[t.id];
        f[u]=1;f[last]=1;f[next]=1;
        a[++tot]=a[last]+a[next]-a[u];
        l[tot]=l[last];r[l[tot]]=tot;
        r[tot]=r[next];l[r[tot]]=tot;
        q.push({tot,a[tot]});
    }
    cout<<ans<<endl;
    // ================ 你的代码到这里结束 ================
    
    #ifdef LOCAL
        auto end_time = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
        cerr << "Time cost: " << duration.count() << " ms" << endl;
    #endif
    
    return 0;
}